package com.yq.servlet;


import com.sun.xml.internal.bind.v2.model.core.ID;
import com.yq.bean.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/view")
public class ServletView extends HttpServlet {



    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        ArrayList<String> cards = (ArrayList<String>) request.getSession().getAttribute("cards");
        if (cards==null){
            response.getWriter().println("还没有商品加入购物车");
            response.setHeader("refresh","2;url="+request.getContextPath()+"/index");
            // response.sendRedirect(request.getContextPath()+"/index");
            return;
        }
        List<Product> products = (List<Product>) getServletContext().getAttribute("products");
        for (String card : cards) {
            for (Product product : products) {
                if (card.equals(product.getId())){
                    response.getWriter().println(product+"<br>");
                }
            }
        }
        response.getWriter().println("<a href="+request.getContextPath()+"/index" +">返回首页</a>");


    }
}
